using Proto;

namespace sgf.server
{
    public abstract class MessageRpcHandler<TBus,T, TR> : IMessageHandler where T : IRequestMsg
        where TR : IResponseMsg, new()
        where TBus : BusActor
    {
        public async Task<IResponseMsg> Run(BusActor busActor, IContext contex)
        {
            if (busActor is not TBus)
            {
                return new TR
                {
                    ErrorCode = 1
                };
            }

            return await Handle((TBus)busActor, (T)contex.Message, contex);
        }

        protected abstract Task<TR> Handle(TBus busActor, T requestMsg, IContext context);

        public Type GetMessageType()
        {
            return typeof(T);
        }
    }
}